package com.polito.mad.bikeponyapp.persistence;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class BikePonySQLiteHelper extends SQLiteOpenHelper{

	public static final String DELIVERY_ITEM_DB_TABLE_NAME = "deliveri_item";
	public static final String QUESTION_DB_TABLE_NAME = "question";
	public static final String BIKE_PONY_DB_NAME = "bike_pony.db";
	private static final String DELIVERY_ITEM_DB_SQL_CREATE="create table "+DELIVERY_ITEM_DB_TABLE_NAME+" (" +
			"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
			"deliveryId LONG," +
			"address TEXT NOT NULL," +
			"type INT NOT NULL," +
			"additionalInfo	TEXT NULL," +
			"estimatedTime LONG NOT NULL," +
			"latitude DOUBLE NOT NULL," +
			"longitude DOUBLE NOT NULL," +
			"_order INTEGER NOT NULL," +
			"image_file_name TEXT NULL)";
	
	private static final String QUESTION_DB_SQL_TABLE_CREATE = "create table "+QUESTION_DB_TABLE_NAME+" ("+
			"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
			"questionId LONG NOT NULL,"+
			"_text TEXT NOT NULL)";
	
	public BikePonySQLiteHelper(Context context,CursorFactory factory, int version) {
		super(context, BIKE_PONY_DB_NAME,factory, version);
	}
	
	public BikePonySQLiteHelper(Context context,CursorFactory factory, int version,DatabaseErrorHandler errorHandler) {
		super(context, BIKE_PONY_DB_NAME, factory, version,errorHandler);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DELIVERY_ITEM_DB_SQL_CREATE);
		db.execSQL(QUESTION_DB_SQL_TABLE_CREATE);
		
	}
	@Override
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE "+DELIVERY_ITEM_DB_TABLE_NAME);
		db.execSQL("DROP TABLE "+QUESTION_DB_TABLE_NAME);
		onCreate(db);
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE "+DELIVERY_ITEM_DB_TABLE_NAME);
		db.execSQL("DROP TABLE "+QUESTION_DB_SQL_TABLE_CREATE);
		onCreate(db);
		
	}

}
